package com.itheima.controller;

import com.github.pagehelper.PageInfo;
import com.itheima.controller.results.Code;
import com.itheima.controller.results.Result;
import com.itheima.domain.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//restfulf风格(风格，而不是规范)
//相当于@Controller和@Responsebody
@RestController
//公共的映射路径
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    //新增post提交
    @PostMapping
    public Result save(@RequestBody User user){
        //System.out.println("save获得"+user);
        boolean flag = userService.save(user);
        Result result = new Result();
        if (flag){
            result.setCode(Code.SAVE_OK);
            result.setFlage(true);
        }else {
            result.setCode(Code.SAVE_ERROR);
            result.setFlage(false);
        }
        return result;
    }

    //修改put提交
    @PutMapping
    public Result update(@RequestBody User user){
        //System.out.println("update"+user);
        boolean flage = userService.update(user);
        Result result = new Result();
        if (flage){
            result.setCode(Code.UPDATE_OK);
            result.setFlage(true);
        }else {
            result.setCode(Code.UPDATE_ERROR);
            result.setFlage(false);
        }
        return result;
    }

    //删除delete提交(不往user封装由于是通过路径传值，也需要加上@PathVariable)
    @DeleteMapping("/{uuid}")
    public Result delete(@PathVariable Integer uuid){
        //System.out.println("delete"+uuid);
        boolean flage = userService.delete(uuid);
        Result result = new Result();
        if (flage){
            result.setCode(Code.DELETE_OK);
            result.setFlage(true);
        }else {
            result.setCode(Code.DELETE_ERROR);
            result.setFlage(false);
        }
        return result;
    }

    //根据id查询单个(由于是通过路径传值，需要加上@PathVariable)
    @GetMapping("/{uuid}")
    public Result get(@PathVariable Integer uuid){
        //System.out.println("get获得"+uuid);
        User user = userService.get(uuid);
        Result result = new Result();
        if (user!=null){
            result.setCode(Code.GET_OK);
            result.setData(user);
            result.setFlage(true);
        }else {
            result.setCode(Code.GET_ERROR);
            result.setFlage(false);
        }
        return result;
    }

    //分页查询(没有查全部，由于是通过路径传值，需要加上@PathVariable)
    @GetMapping("/{page}/{size}")
    public Result getAll(@PathVariable Integer page,@PathVariable Integer size){
        //System.out.println("getAll获得"+"page:"+page+",size:"+size);
        PageInfo<User> pageInfo = userService.getAll(page, size);
        Result result = new Result();
        if (pageInfo!=null){
            result.setCode(Code.GET_OK);
            result.setData(pageInfo);
            result.setFlage(true);
        }else {
            result.setCode(Code.GET_ERROR);
            result.setFlage(false);
        }
        return result;
    }

    //登录发post请求，防止get请求可以通过url直接查看到用户的账号和密码
    @PostMapping("/login")  //和save方法进行区分
    public Result login(String userName,String password){
        //System.out.println("login:"+userName+"和"+password);
        User user = userService.login(userName, password);
        Result result = new Result();
        if (user!=null){
            result.setCode(Code.LOGIN_OK);
            result.setData(user);
            result.setFlage(true);
        }else {
            result.setCode(Code.LOGIN_ERROR);
            result.setFlage(false);
        }
        return result;
    }


}
